package com.AC.ACAlibaba;

import org.junit.Test;

public class test1 {
    private class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode reverseList(ListNode head){
        if(head == null || head.next == null)
            return head;

        ListNode dummy = new ListNode(0), prev = dummy, curr = head, next;

        int len = 0;
        dummy.next = head;

        while(head!=null){
            len++;
            head = head.next;
        }

        int time = len / 2;

        for(int i = 0; i<time; i++){
            next = curr.next;
            curr.next = next.next;
            next.next = prev.next;
            prev.next = next;
            prev = curr;
            curr = curr.next;
        }
        return dummy.next;
    }

    public void outList(ListNode head){
        while(head!=null){
            System.out.print(head.val + " ");
            head = head.next;
        }
    }

    @Test
    public void test(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        outList(reverseList(node1));
    }
}
